package com.maolong.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maolong.pojo.fuction.result.Result;
import com.maolong.pojo.dto.ModuleDTO;
import com.maolong.pojo.entity.Menu;
import com.maolong.pojo.vo.MenuNodeVo;
import com.maolong.pojo.vo.MenuVo;
import com.maolong.service.MenuService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 这里之前写错了，现在进行修改，
 * 可能会用到menuService里面的东西，其实本质上也是操作菜单
 */
@RestController
@Slf4j
@Api(tags = "菜单管理")
@RequestMapping("/Module")
public class ModuleController {


    @Autowired
    MenuService menuService;

    @PostMapping("/list")
    public Result<List<MenuVo>> list(){
        log.info("获取菜单树");
        List<MenuVo> menuVos = menuService.selectMenus();
        return Result.success(menuVos);
    }

    @PostMapping("/nodes")
    public Result<List<MenuNodeVo>> nodes(){
        log.info("获取父节点信息");
        List<MenuNodeVo> menus = menuService.selectNodes();
        return Result.success(menus);
    }

    @GetMapping("/get/{id}")
    public Result getNode(@PathVariable Integer id){
        log.info("获取节点信息,{}",id);
//        Module nodeData = moduleService.getNodeData(id);
        Menu byId = menuService.getById(id);

        return Result.success(byId);

    }

    @PostMapping("/save")
    public Result save(@RequestBody ModuleDTO moduleDTO){
        log.info("保存节点信息,{}",moduleDTO);



        Menu menu = new Menu().builder()
                .menuName(moduleDTO.getModuleName())
                .url(moduleDTO.getModuleUrl())
                .pId(Long.valueOf(moduleDTO.getParentId()))
                .orderValue(moduleDTO.getModuleOrder())
                .hasThird("N")
                .icon(moduleDTO.getModuleIcon())
                .build();

        menuService.saveOrUpdate(menu,new QueryWrapper<Menu>().eq("menu_name",menu.getMenuName()));
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(Integer ids){
        log.info("删除节点信息,{}",ids);
        menuService.delete(ids);
//        moduleService.removeById(ids);
        return Result.success();
    }
}
